package cultu.customer.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import cultu.core.message.Context;
import cultu.customer.dao.OrderDAO;
import cultu.customer.message.Order;
import cultu.customer.message.OrderCriteria;
import cultu.customer.message.OrderList;
import cultu.customer.message.OrderScenario;
import cultu.customer.service.OrderManager;
import cultu.supplier.message.Product;
import cultu.supplier.service.ProductService;

/**
 * Order service implementation.
 * @author Marc-Antoine
 *
 */
@Transactional
public class OrderManagerImpl implements OrderManager {

  /**
   * Order Dao.
   */
  private OrderDAO orderDao;
  
  /**
   * Product service.
   */
  private ProductService productService;

  /**
   * Sets Order Dao.
   * @param dao the dao to set.
   */
  public void setOrderDao(final OrderDAO dao){
    this.orderDao = dao;
  }
  
  /**
   * Sets Order Dao.
   * @param dao the dao to set.
   */
  public void setProductService(final ProductService productService){
    this.productService = productService;
  }

  /**
   * {@inheritDoc}
   */
  @Transactional(readOnly = true)
  public Order getOrder(final Context pContext, final OrderScenario pScenario, final Order pKey) {

    Order result = orderDao.get(pKey, pScenario);

    return result;
  }

  /**
   * {@inheritDoc}
   */
  @Transactional
  public Order createOrder (final Context pContext, final OrderScenario pScenario, final Order pOrder) {

    Product product = productService.getProductById(pOrder.getProduct().getHjid());
    pOrder.setProduct(product);
    
    Order result = orderDao.create(pOrder);

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Order deleteOrder(final Context pContext, final OrderScenario pScenario, final Order pKey) {
    Order result = orderDao.delete(pKey);
    return result;
  }

  /**
   * {@inheritDoc}
   */
  public Order updateOrder(final Context pContext, final OrderScenario pScenario, final Order pOrder) {

    Order result = orderDao.update(pOrder);
    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OrderList findOrders(final Context pContext, final OrderScenario pScenario, final OrderCriteria pCriteria) {

    OrderList result = new OrderList();

    if (pCriteria != null) {
      List<Order> myList = orderDao.find(pCriteria, pScenario);
      result.getOrders().addAll(myList);
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OrderList deleteOrders(final Context pContext, final OrderScenario pScenario, final OrderList pList) {

    OrderList result = new OrderList();

    if (pList != null) {
      for (Order myUser : pList.getOrders()) {
        result.getOrders().add(deleteOrder(pContext, pScenario, myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OrderList createOrders(final Context pContext, final OrderScenario pScenario, final OrderList pList) {

    OrderList result = new OrderList();

    if (pList != null) {
      for (Order myUser : pList.getOrders()) {
        result.getOrders().add(createOrder(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

  /**
   * {@inheritDoc}
   */
  public OrderList updateOrders(final Context pContext, final OrderScenario pScenario, final OrderList pList) {

    OrderList result = new OrderList();

    if (pList != null) {
      for (Order myUser : pList.getOrders()) {
        result.getOrders().add(updateOrder(pContext, pScenario,  myUser));
      }
    }

    return result;
  }

}
